﻿Option Explicit On
Option Strict On

''' <summary>
''' Formulario Agregar Recordatorio: Para agregar recordatorios a la cita actual
''' </summary>
''' <author>Jose Manuel Rizo</author>

Public Class AgregarRecordatorio
    Private _conexionBBDD As BBDD
    Private _recordatorioInicial As String
    Private _cita As Cita
    Private _aceptado As Boolean
    Private _sinCita As Boolean


    Private Sub AgregarRecordatorio_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        _sinCita = True

        'Comprobamos que haya alguna fila del datagrid selecionada
        If Principal.dgvCitasUsuario.SelectedRows.Count = 0 Then

            MsgBox("No hay ninguna cita seleccionada en el formulario principal", MsgBoxStyle.Information, Me.Text)
        Else

            _conexionBBDD = New BBDD
            _aceptado = False

        End If
    End Sub


    Public Sub MostrarRecordatorio(ByVal codigoCita As Integer)
        Try

            _conexionBBDD.Conectar()
            _cita = _conexionBBDD.DevuelveCita("Select * from cita where cod = " & codigoCita)
            _conexionBBDD.Desconectar()

            _recordatorioInicial = _cita.Recordatorio

            'Insertar Texto en rtbRecordatorio
            rtbRecordatorio.Text = _recordatorioInicial
            _sinCita = False

        Catch ex As Exception
            MsgBox("Hubo un error conectando con la Base de Datos", MsgBoxStyle.Information, Me.Text)
        End Try
    End Sub


    Private Sub btAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btAceptar.Click
        _aceptado = True

        'Cerrar ventana.
        Me.Close()
    End Sub


    Private Sub btCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCancelar.Click
        Me.Close()
    End Sub


    Private Sub cerrando_From(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FormClosing

        If Not _sinCita And _aceptado And _recordatorioInicial <> rtbRecordatorio.Text Then

            'Guardamos los cambios
            _cita.Recordatorio = rtbRecordatorio.Text

            _conexionBBDD.Conectar()

            If _conexionBBDD.ModificaCita(_cita) Then
                MsgBox("Recordatorio modificado", MsgBoxStyle.Information, Me.Text)
            Else
                MsgBox("Hubo un error conectando con la Base de Datos", MsgBoxStyle.Information, Me.Text)
            End If

            _conexionBBDD.Desconectar()
            Principal.ActualizarCitas()

        End If
    End Sub



End Class